package com.adv.ems.controller.client;

import com.adv.ems.constants.SystemConst;
import com.adv.ems.model.dto.APIRequest;
import com.adv.ems.model.dto.APIResponse;
import com.adv.ems.model.param.ReportEnergyExcelParam;
import com.adv.ems.model.param.ReportMeterExcelParam;
import com.adv.ems.model.param.ReportTemplateExcelParam;
import com.adv.ems.model.vo.ReportEnergyExcelVO;
import com.adv.ems.model.vo.ReportMeterExcelVO;
import com.adv.ems.model.vo.ReportTemplateExcelVO;
import com.adv.ems.service.ReportService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/ems/client/energy/report")
public class ReportController {
    @Resource
    ReportService reportService;

    @PostMapping("/subject/energy/excel")
    public APIResponse<ReportEnergyExcelVO> energyExcel(@RequestHeader(name = SystemConst.Header.ACCEPT_LANGUAGE, defaultValue = SystemConst.Header.ZH_CN) String acceptLanguage,
                                                        @RequestBody APIRequest<ReportEnergyExcelParam> request) {
        return APIResponse.<ReportEnergyExcelVO>build().content(reportService.energyExcel(request.getContent(), acceptLanguage));
    }

    @PostMapping("/meter/energy/excel")
    public APIResponse<ReportMeterExcelVO> deviceExcel(@RequestHeader(name = SystemConst.Header.ACCEPT_LANGUAGE, defaultValue = SystemConst.Header.ZH_CN) String acceptLanguage,
                                                        @RequestBody APIRequest<ReportMeterExcelParam> request) {
        return APIResponse.<ReportMeterExcelVO>build().content(reportService.deviceExcel(request.getContent(), acceptLanguage));
    }

    @PostMapping("/template/excel")
    public APIResponse<ReportTemplateExcelVO> templateExcel(@RequestHeader(name = SystemConst.Header.ACCEPT_LANGUAGE, defaultValue = SystemConst.Header.ZH_CN) String acceptLanguage,
                                                            @RequestBody APIRequest<ReportTemplateExcelParam> request) {
        return APIResponse.<ReportTemplateExcelVO>build().content(reportService.templateExcel(request.getContent(), acceptLanguage));
    }

    @RequestMapping("/template/excel/download")
    public void templateExcelDownload(HttpServletResponse response,
                                      @RequestParam(name = "fromDate") String fromDate,
                                      @RequestParam(name = "toDate") String toDate,
                                      @RequestParam(name = "reportTemplateId") Long reportTemplateId) {
        ReportTemplateExcelParam param = new ReportTemplateExcelParam();
        param.setReportTemplateId(reportTemplateId);
        param.setFromDate(fromDate);
        param.setToDate(toDate);
        reportService.templateExcelDownload(param, response);
    }
}
